
/**
 * Created by ZhongXu on 2017/7/5.
 */
$(document).ready(function(){
    var VAL ={
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FTPTOLRELA :'FTPTOLRELA',
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FBPBISDAT: 'FBPBISDAT',
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        $rln_lvl  : $("#rln_lvl"),
        $sour_tool_id_fk  : $("#sour_tool_id_fk"),
        $dest_tool_id_fk  : $("#dest_tool_id_fk"),
        $rln_stat         : $("#rln_stat"),
        $evt_note         : $("#evt_note"),
        buttons:{
            $query_btn        :$("#query_btn"),
            $register_btn     :$("#register_btn"),
           $load_model_btn   :$("#load_model_btn"),
            $cancel_btn       :$("#cancel_btn"),
            $clear_btn        :$("#clear_btn"),
            $update_btn       :$("#update_btn"),
            $commit_btn       :$("#commit_btn"),
        },
        grids:{
            $settingDiv:$("#settingDiv"),
            $settingGrd:$("#settingGrd"),
            $settingPg:$("#settingPg")
        }
    };
    function initSettingGrid() {
        domObj.grids.$settingGrd.jqGrid({
            url : "",
            datatype : "local",
            mtype : "POST",
            height : 340,
            width : $("#settingDiv").width() * 0.99,
            autowidth : true,
            shrinkToFit:false,
            autoScroll: false,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rowNum : 20,
            emptyrecords : true,
            //multiselect : true, 多选
            pager : domObj.grids.$settingPg,
            colModel : [
                /*  {
                 name : 'rln_lvl',
                 index : 'rln_lvl',
                 label : '关系级别',
                 width : 150
                 },*/
                {
                    name : 'rln_stat',
                    index : 'rln_stat',
                    label : '关系状态',
                    width : 180
                }, {
                    name : 'sour_tool_id_fk',
                    index : 'sour_tool_id_fk',
                    label : '子设备代码',
                    width : 210
                },
                {
                    name : 'dest_tool_id_fk',
                    index : 'dest_tool_id_fk',
                    label : '主设备代码',
                    width : 210
                },
                {
                    name : 'evt_usr',
                    index : 'evt_usr',
                    label : '创建人',
                    width : 210
                }, {
                    name : 'evt_timestamp',
                    index : 'evt_timestamp',
                    label : '创建时间',
                    width : 260
                }, {
                    name : 'evt_note',
                    index :'evt_note',
                    label : '备注',
                    width : 320
                }],
            onSelectRow : function(rowId) {
                rowData = domObj.grids.$settingGrd.jqGrid(
                    "getRowData", rowId);
                /* SelectDom.setSelect(domObj.$rln_lvl,
                 rowData.rln_lvl);*/
                SelectDom.setSelect(domObj.$sour_tool_id_fk,
                    rowData.sour_tool_id_fk);
                SelectDom.setSelect(domObj. $dest_tool_id_fk,
                    rowData.dest_tool_id_fk);
                SelectDom.setSelect(domObj.$rln_stat,
                    rowData.rln_stat);
                domObj.$evt_note.val(rowData.evt_note);
                // 不可变
                unableFn();
                $("#update_btn").attr(VAL.ENABLED_ATTR);

            }
        });
    }
    var initSelectFnc = {
        iniLvlSel  :function(){
            var iary,inObj,outObj;
            iary = {
                data_cate : "TRLV",
            };
            inObj = {
                trx_id : VAL.T_FBPBISDAT,
                action_flg : "Q",
                iary : [ iary ]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var tblCnt=comParseInt(outObj.tbl_cnt);
                domObj.$rln_lvl.append("<option value='' > </option>");
                for(var i=0;i<tblCnt;i++){
                    domObj.$rln_lvl.append("<option value='" + outObj.oary[i].data_id + "'>" + outObj.oary[i].data_desc +"</option>");
                }
                domObj.$rln_lvl.select2({
                    theme : "bootstrap"
                });
            }
        },
        iniSourToolSel:function(){
            var iary,inObj,outObj;
                iary = {
                    unit_typ:'UNIT'
                }
                inObj = {
                    trx_id : VAL.T_FBPBISTOL,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    tblCnt = outObj.tbl_cnt;
                    SelectDom.initWithSpace(domObj.$sour_tool_id_fk);
                    var warp = document.createDocumentFragment();
                    for (i = 0; i < tblCnt; i++) {
                        if(tblCnt > 1){
                            oary = outObj.oary[i];
                        }else{
                            oary = outObj.oary[0];
                        }
                        var option = document.createElement("option");
                        option.value = oary.tool_id;
                        option.text = oary.tool_id;
                        warp.appendChild(option);
                    }
                    domObj.$sour_tool_id_fk.append(warp);
                }
        },
        iniStat:function(){
            var warp = document.createDocumentFragment();
            var oary =[{'model':''},{'model':'RUN'},{'model':'DOWN'},{'model':'IDLE'},{'model':'PM'}];
            for(var i=0;i<oary.length;i++){
                var option = document.createElement("option");
                option.value = oary[i].model;
                option.text = oary[i].model;
                warp.appendChild(option);
            }
            domObj.$rln_stat.append(warp);
            domObj.$rln_stat.select2({
                theme : "bootstrap"
            });
        }
    }
    var btnFunc = {
        queryBtnfunc : function() {
            var rlnLvl,sourToolIdFk,destToolIdFk,rlnStat;
         /*   rlnLvl = domObj.$rln_lvl.val();*/
            sourToolIdFk = domObj.$sour_tool_id_fk.val();
            destToolIdFk = domObj.$dest_tool_id_fk.val();
            rlnStat = domObj.$rln_stat.val();
            var iary,inObj, outObj;
            iary = {
            /*    rln_lvl         : rlnLvl,*/
                sour_tool_id_fk : sourToolIdFk,
                dest_tool_id_fk : destToolIdFk,
                rln_stat        : rlnStat
            };
            inObj = {
                trx_id: VAL.T_FTPTOLRELA,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary,"#settingGrd");
            }
        },
        saveBtnfunc : function() {
            var sourToolIdFk,destToolIdFk,rlnStat,evtNote,iary,inObj, outObj;
          /*  rlnLvl = domObj.$rln_lvl.val();*/
            sourToolIdFk = domObj.$sour_tool_id_fk.val();
            destToolIdFk = domObj.$dest_tool_id_fk.val();
            rlnStat = domObj.$rln_stat.val();
            evtNote   = domObj.$evt_note.val();
           /* if (!rlnLvl) {
                showErrorDialog("", "关系级别不能为空");
                return false;
            }*/
            if (!sourToolIdFk) {
                showErrorDialog("", "子设备代码不能为空");
                return false;
            }
            if (!destToolIdFk) {
                showErrorDialog("", "主设备代码不能为空");
                return false;
            }
            if (!rlnStat) {
                showErrorDialog("", "关系状态不能为空");
                return false;
            }
            if (!evtNote) {
                showErrorDialog("", "备注不能为空");
                return false;
            }
            //可编辑说明为新增，不可编辑说明为修改
            actionFlg = domObj.$sour_tool_id_fk.attr("disabled") === "disabled" ? "U" : "A";
            iary={
            /*    rln_lvl          :   rlnLvl,*/
                sour_tool_id_fk  :   sourToolIdFk ,
                dest_tool_id_fk  :   destToolIdFk,
                rln_stat         :   rlnStat ,
                evt_note         :   evtNote
            }
            inObj = {
                trx_id :  VAL.T_FTPTOLRELA,
                action_flg : actionFlg,
                evt_usr    : VAL.EVT_USER ,
                iary : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                if(actionFlg=="A"){
                    //comfunction有
                    newRowId = getGridNewRowID("#settingGrd")
                    domObj.grids.$settingGrd.jqGrid("addRowData", newRowId,
                        outObj.oary[0]);
                    showSuccessDialog("机台关系添加成功");
                    $("#update_btn").attr(VAL.ENABLED_ATTR);
                }else if (actionFlg=="U"){
                    selRowId = domObj.grids.$settingGrd.jqGrid("getGridParam","selrow");
                    domObj.grids.$settingGrd.jqGrid("setRowData",selRowId,outObj.oary[0]);
                    showSuccessDialog("机台关系更新成功");
                    unableFn();
                }
            }
        },
        updateFnc : function () {
            var selRowId;
            selRowId = domObj.grids.$settingGrd.jqGrid("getGridParam",
                "selrow");
            if (!selRowId) {
                showErrorDialog("", "请选择需要修改的机台关系");
                return false;
            }
           enableFn();

            domObj.$sour_tool_id_fk.attr(VAL.DISABLED_ATTR);
            domObj.$dest_tool_id_fk.attr(VAL.DISABLED_ATTR);
            domObj.$rln_stat.attr(VAL.DISABLED_ATTR);
        },
        cancelBtnfunc: function() {
            var selRowId, rowData, iary, inObj, outObj;
            //获得选中的行ID
            selRowId = domObj.grids.$settingGrd.jqGrid("getGridParam",
                "selrow");
            if (!selRowId) {
                showErrorDialog("", "请选择需要删除的机台关系");
                return false;
            }
            $("#cancel_btn").showCallBackWarnningDialog({
                errMsg : "是否删除该机台关系,请确认!!!!",
                callbackFn : function(data) {
                    if (data.result == true) {
                        //获得选中行的数据
                        rowData = domObj.grids.$settingGrd.jqGrid(
                            "getRowData", selRowId);
                        iary = {
                            sour_tool_id_fk  :   rowData.sour_tool_id_fk ,
                            dest_tool_id_fk  :   rowData.dest_tool_id_fk,
                            rln_stat         :   rowData.rln_stat
                        };
                        inObj = {
                            trx_id :  VAL.T_FTPTOLRELA,
                            action_flg : "D",
                            iary : [iary]
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("删除机台关系成功");
                            domObj.grids.$settingGrd.jqGrid(
                                "delRowData", selRowId);
                            //删除完后页面除了Parameter信息，清空
                        }
                    }
                }
            });
        },
        clearBtnfunc: function() {
        /*    SelectDom.setSelect(domObj.$rln_lvl,'')*/;
            SelectDom.setSelect(domObj.$sour_tool_id_fk,'');
            SelectDom.setSelect(domObj. $dest_tool_id_fk,'');
            SelectDom.setSelect(domObj.$rln_stat,'');
            domObj.$evt_note.val('');
            domObj.grids.$settingGrd.jqGrid("clearGridData");
            enableFn();
        },
        commitBtnfunc:function() {
            var iary,inObj,outObj
            var Data = domObj.grids.$settingGrd.jqGrid("getRowData");
            if(Data.length<1){
                showErrorDialog("", "设定信息表不能为空");
                return;
            }
            iary = {
                ftptolrelateIBs  : Data
            };
            inObj = {
                trx_id :  VAL.T_FTPTOLRELA,
                action_flg : "F",
                iary : [iary],
                evt_usr    : VAL.EVT_USER ,
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                showSuccessDialog("导入提交成功");
            }
        }
    }
    var iniButtonAction =function(){
        domObj.buttons.$query_btn.click(function(){
            btnFunc.queryBtnfunc();
        });
        domObj.buttons.$register_btn.click(function(){
            btnFunc.saveBtnfunc();
        });
        domObj.buttons.$cancel_btn.click(function(){
            btnFunc.cancelBtnfunc();
        });
        domObj.buttons.$update_btn.click(function(){
            btnFunc.updateFnc();
        });
        domObj.buttons.$clear_btn.click(function(){
            btnFunc.clearBtnfunc();
        });
        domObj.buttons. $commit_btn.click(function(){
            btnFunc.commitBtnfunc();
        });
    };

    domObj.$rln_lvl.on("select2:select",function(){
        SelectDom.setSelect(domObj.$sour_tool_id_fk,"");
        initSelectFnc.iniSourToolSel();
    });
    domObj.$sour_tool_id_fk.on("select2:select",function(){
        var ToolId = domObj.$sour_tool_id_fk.val();
        iary = {
            tool_id:ToolId
        }
        inObj = {
            trx_id : VAL.T_FBPBISTOL,
            action_flg : "S",
            iary : [ iary ]
        };
        outObj = comTrxSubSendPostJson(inObj);
        if (outObj.rtn_code === VAL.NORMAL) {
            tblCnt = outObj.tbl_cnt;
            SelectDom.initWithSpace(domObj.$dest_tool_id_fk);
            var warp = document.createDocumentFragment();
            for (i = 0; i < tblCnt; i++) {
                if(tblCnt > 1){
                    oary = outObj.oary[i];
                }else{
                    oary = outObj.oary[0];
                }
                var option = document
                    .createElement("option");
                option.value = oary.tool_id;
                option.text = oary.tool_id;
                warp.appendChild(option);
            }
            domObj.$dest_tool_id_fk.append(warp);
            SelectDom.setSelect(domObj.$dest_tool_id_fk,
                outObj.oary[0].tool_id);
        }
    });
    function unableFn() {
        domObj.$rln_lvl.attr(VAL.DISABLED_ATTR);
        domObj.$sour_tool_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$dest_tool_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$rln_stat.attr(VAL.DISABLED_ATTR);
        domObj.$evt_note.attr(VAL.DISABLED_ATTR);
    }

    // 将控件变成都可修改
    function enableFn() {
        domObj.$rln_lvl.attr(VAL.ENABLED_ATTR);
        domObj.$sour_tool_id_fk.attr(VAL.ENABLED_ATTR);
        domObj.$rln_stat.attr(VAL.ENABLED_ATTR);
        domObj.$evt_note.attr(VAL.ENABLED_ATTR);
    }
    function init() {
        domObj.$dest_tool_id_fk.prop("disabled", true);
        initSelectFnc.iniLvlSel();
        initSelectFnc.iniSourToolSel();
        initSelectFnc.iniStat();
    }
    var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });

    };
    function initFunc(){
        init();
        iniButtonAction();
        initSettingGrid();
        otherActionBind();
    }
    initFunc();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#settingDiv").width();
        offsetBottom =  $(window).height() - $("#settingDiv").offset().top;
        $("#settingDiv").height(offsetBottom * 0.95);
        $("#settingGrd").setGridWidth(divWidth * 0.99);
        $("#settingGrd").setGridHeight(offsetBottom * 0.99 - 51);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });

});